// Parser.cpp : Implementation of CParser

#include "stdafx.h"
#include "Parser.h"
#include ".\parser.h"

bool parse(const char* str, std::vector<MergeToken>& v, string commentchars = "//")
{
	VectorBuilder::Clear();

	std::string text = str;
	MergeParserGrammar g;

	string comments = commentchars;
	parse_info<> info = parse(str,  g);

	typedef std::vector<MergeToken>::iterator iterator_t;
	for(iterator_t iter = VectorBuilder::begin(); iter != VectorBuilder::end(); iter++)
	{
		v.push_back(*iter);
	}
	
	return info.full;
}
// CParser


STDMETHODIMP CParser::Parse(BSTR text, IParserCallback* pCallback)
{
	
	if (pCallback == NULL)
		return E_POINTER;

	COLE2T pszText(text);
	CAtlString szText = pszText;
	
	std::vector<MergeToken> tokens;
	bool bResult = parse(szText, tokens);

	if (!bResult)
	{
		Error("Parsing failed!");
		return E_FAIL;
	}
	
	pCallback->OnBeginParse();
	typedef std::vector<MergeToken>::iterator iterator_t;
	for(iterator_t iter = tokens.begin(); iter != tokens.end(); iter++)
	{
		pCallback->OnParse(CComBSTR(iter->Text.c_str()), iter->TokenType);
	}
	pCallback->OnEndParse();

	return S_OK;
}
